import utilsNumber from './number'
// 数组去重  by shw
const arrDedup = function (arr = []) {
	return Array.from(new Set(arr))
}
// 查找数组满足条件的元素,且返回指定值  by shw
const appointedMap = function (arr = [], filter) {
	const res = []
	arr.forEach((item, i) => {
		const data = filter(item, i)
		if (undefined !== data) {
			res.push(data)
		}
	})
	return res
}
// 数组求和 by shw
const arrSum = function (arr, fn, decimal = 2) {
	const sum = arr.reduce((pre, cur) => {
		return utilsNumber.toNumber(pre) + utilsNumber.toNumber(fn(cur))
	}, 0)
	return utilsNumber.toFixed(sum, decimal)
}
// 数组扁平化 by shw
const arrFlatten = function (arr, fn) {
	return arr.reduce((pre, cur) => {
		const child = fn(cur)
		return pre.concat(Array.isArray(child) ? [cur].concat(arrFlatten(child, fn)) : cur)
	}, [])
}
export default {
	arrDedup,
	appointedMap,
	arrSum,
	arrFlatten
}
